package com.lancoo.answer.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvViewEditInputBinding

/**
 * Created by wealive on 2021/9/14.
 *
 *
 */
class EditInputView(val type: String, context: Context) : PopupWindow(context),
    View.OnClickListener, TextWatcher {
    var binding: EvViewEditInputBinding = EvViewEditInputBinding.inflate(LayoutInflater.from(context))

    init {
//        val contentView = LayoutInflater.from(context).inflate(R.layout.ev_view_edit_input, null)
        //        contentView.apply {
        binding.edt.requestFocus()
        binding.edt.addTextChangedListener(this@EditInputView)
        binding.ivClear.setOnClickListener(this@EditInputView)
        binding.tvConfirm.setOnClickListener(this@EditInputView)
        when (type) {
            "edit" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_edit_black)
            "front" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_add_front_black)
            "back" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_add_back_black)
        }
        binding.iv3rd.tag = type
//            iv.setOnClickListener(this@EditInputView)
//            iv_2nd.setOnClickListener(this@EditInputView)
//            iv_3rd.setOnClickListener(this@EditInputView)
//        }
        this.contentView = binding.root
        this.width = WindowManager.LayoutParams.MATCH_PARENT
        this.height = WindowManager.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable = false
        this.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        this.isFocusable = true
        this.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        update()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_confirm -> dismiss()
            R.id.iv_clear -> binding.edt.setText("")
//            R.id.iv -> {
//                contentView.ll.visibility = View.GONE
//                when (contentView.iv.tag) {
//                    "edit" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_edit_black)
//                        contentView.iv_3rd.tag = "edit"
//                    }
//                    "front" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_add_front_black)
//                        contentView.iv_3rd.tag = "front"
//                    }
//                    "back" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_add_back_black)
//                        contentView.iv_3rd.tag = "back"
//                    }
//                }
//            }
//            R.id.iv_2nd -> {
//                contentView.ll.visibility = View.GONE
//                when (contentView.iv_2nd.tag) {
//                    "edit" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_edit_black)
//                        contentView.iv_3rd.tag = "edit"
//                    }
//                    "front" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_add_front_black)
//                        contentView.iv_3rd.tag = "front"
//                    }
//                    "back" -> {
//                        contentView.iv_3rd.setImageResource(R.mipmap.aibk_icon_gct_add_back_black)
//                        contentView.iv_3rd.tag = "back"
//                    }
//                }
//            }
//            R.id.iv_3rd -> {
//                when (contentView.iv_3rd.tag) {
//                    "edit" -> {
//                        contentView.iv.setImageResource(R.mipmap.aibk_icon_gct_add_front_black)
//                        contentView.iv_2nd.setImageResource(R.mipmap.aibk_icon_gct_add_back_black)
//                        contentView.iv.tag = "front"
//                        contentView.iv_2nd.tag = "back"
//                    }
//                    "front" -> {
//                        contentView.iv.setImageResource(R.mipmap.aibk_icon_gct_edit_black)
//                        contentView.iv_2nd.setImageResource(R.mipmap.aibk_icon_gct_add_back_black)
//                        contentView.iv.tag = "edit"
//                        contentView.iv_2nd.tag = "back"
//                    }
//                    "back" -> {
//                        contentView.iv.setImageResource(R.mipmap.aibk_icon_gct_edit_black)
//                        contentView.iv_2nd.setImageResource(R.mipmap.aibk_icon_gct_add_front_black)
//                        contentView.iv.tag = "edit"
//                        contentView.iv_2nd.tag = "front"
//                    }
//                }
//                contentView.ll.visibility.let { if (it == View.VISIBLE) View.GONE else View.VISIBLE }
//            }
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        if (s?.toString()!!.isEmpty()) {
            binding.ivClear.visibility = View.GONE
        } else {
            binding.ivClear.visibility = View.VISIBLE
        }
    }
}